﻿using CDMS.BLL;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CDMS.Common;
using CDMS.Entities;
using DevExpress.XtraEditors;
using DevExpress.LookAndFeel;
using DevExpress.XtraEditors.Controls;
using CDMS.Utilities;
namespace CDMS.Forms
{
    public partial class frmCustomerInfo : Form
    {
        Customer_BLL customer_BLL;
        Customer customer;
        int customerID;

        public frmCustomerInfo(int customerID)
        {
            InitializeComponent();
            this.DialogResult = DialogResult.Cancel;
            this.customerID = customerID;
            SetTitle(customerID);
            LoadLookUpEdit_Position();
            LoadLookUpEdit_Vocative();
            LoadLookUpEdit_City();
            LoadLookUpEdit_CustomerGroup();
            LoadLookUpEdit_CustomerType();
            LoadLookUpEdit_MasketSegment();
            LoadLookUpEdit_Nationlaty();
            LoadLookUpEdit_SourceInfo();
            LoadLookUpEdit_User();
            DataToForm();
        }
        private void SetTitle(int iD)
        {
            lblTitle.Text = iD == 0 ? "Thêm " : "Sửa ";
            lblTitle.Text += " thông tin khách hàng";
            this.Text = lblTitle.Text;
        }

        bool CheckValid()
        {
            dxErrorProvider1.ClearErrors();
            if (string.IsNullOrEmpty(txtCode.Text.Trim()))
            {
                ErrorProvider_SetError(txtCode, "Mã không được để trống!.");
                return false;
            }
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                ErrorProvider_SetError(txtName, "Tên không được để trống!.");
                return false;
            }
            return true;
        }
        void ErrorProvider_SetError(Control control, string errorText)
        {
            dxErrorProvider1.SetError(control, errorText);
            dxErrorProvider1.SetIconAlignment(control, ErrorIconAlignment.TopRight);
        }
        public void DataToForm()
        {
            customer_BLL = new Customer_BLL();
            customer = customer_BLL.GetOne(this.customerID);
            if (customer != null)
            {
                txtCode.Text = customer.Customer_Code;
                txtName.Text = customer.Customer_Name;
                txtNameEN.Text = customer.Customer_NameEN;
                if (customer.Customer_DOB == DateTime.MinValue)
                    dateEditDOB.EditValue = null;
                else
                    dateEditDOB.EditValue = customer.Customer_DOB;
                txtAddress.Text = customer.Customer_Address;
                txtAddressShip.Text = customer.Customer_AddressShip;
                txtPhone.Text = customer.Customer_Phone;
                txtMobile.Text = customer.Customer_Mobile;
                txtEmail.Text = customer.Customer_Email;
                txtContact.Text = customer.Customer_Contact;
                txtContactPhone.Text = customer.Customer_ContactPhone;
                lptPosition.EditValue = customer.Customer_Position;
                lptCity.EditValue = customer.Customer_CityID;
                lptCustomerGroup.EditValue = customer.Customer_CustomerGroupID;
                lptCustomerType.EditValue = customer.Customer_CustomerTypeID;
                lptMasketSegment.EditValue = customer.Customer_MarketSegmentID;
                lptNationlaty.EditValue = customer.Customer_NationlatyID;
                lptSourceInfo.EditValue = customer.Customer_SourceInfoID;
                lptUser.EditValue = customer.Customer_UserID;
                lptVocative.EditValue = customer.Customer_Vocative;
                txtRegistrationCode.Text = customer.Customer_RegistrationNo;
                txtRegistrationName.Text = customer.Customer_RegistrationName;
                txtRegistrationAddress.Text = customer.Customer_RegistrationAddress;
                txtTaxCode.Text = customer.Customer_TaxCode;
                txtWebsite.Text = customer.Customer_Website;
                chkAcceptEmail.Checked = customer.Customer_AcceptEmail;
            }
        }
        public static DateTime GetDateTime(string strDate)
        {
            DateTime retVal;
            if (!DateTime.TryParseExact(strDate, "dd/MM/yyyy", null, System.Globalization.DateTimeStyles.None, out retVal))
                retVal = DateTime.MinValue; //"01/01/0001"
            return retVal;

        }
        void FormToData()
        {
            customer = new Customer();
            customer.Customer_ID = customerID;
            customer.Customer_Code=txtCode.Text;
            customer.Customer_Name=txtName.Text;
            customer.Customer_NameEN=txtNameEN.Text;
            customer.Customer_DOB = GetDateTime(dateEditDOB.Text);
            customer.Customer_Address=txtAddress.Text;
            customer.Customer_AddressShip=txtAddressShip.Text;
            customer.Customer_Phone=txtPhone.Text;
            customer.Customer_Mobile=txtMobile.Text;
            customer.Customer_Email=txtEmail.Text;
            customer.Customer_Contact=txtContact.Text;
            customer.Customer_ContactPhone=txtContactPhone.Text;
            customer.Customer_Position=Convert.ToInt32(lptPosition.EditValue);
            customer.Customer_CityID=Convert.ToInt32(lptCity.EditValue);
            customer.Customer_CustomerGroupID=Convert.ToInt32(lptCustomerGroup.EditValue);
            customer.Customer_CustomerTypeID=Convert.ToInt32(lptCustomerType.EditValue);
            customer.Customer_MarketSegmentID=Convert.ToInt32(lptMasketSegment.EditValue);
            customer.Customer_NationlatyID=Convert.ToInt32(lptNationlaty.EditValue);
            customer.Customer_SourceInfoID=Convert.ToInt32(lptSourceInfo.EditValue);
            customer.Customer_UserID=Convert.ToInt32(lptUser.EditValue);
            customer.Customer_Vocative=Convert.ToInt32(lptVocative.EditValue);
            customer.Customer_RegistrationNo=txtRegistrationCode.Text;
            customer.Customer_RegistrationName=txtRegistrationName.Text;
            customer.Customer_RegistrationAddress=txtRegistrationAddress.Text;
            customer.Customer_TaxCode=txtTaxCode.Text;
            customer.Customer_Website=txtWebsite.Text;
            customer.Customer_AcceptEmail=chkAcceptEmail.Checked;
            
        }
        void LoadLookUpEdit_Position()
        {

            DataTable dt = new DataTable();
            Category_BLL category_BLL = new Category_BLL(Entities.EnumCategory.Position);
            dt = category_BLL.GetALLActivated();
            DataRow dr = dt.NewRow();
            dr["Position_ID"] = 0;
            dr["Position_Name"] = "Tất cả";
            dt.Rows.InsertAt(dr, 0);
            lptPosition.Properties.DataSource = dt;
            lptPosition.Properties.DisplayMember = "Position_Name";
            lptPosition.Properties.ValueMember = "Position_ID";
            LookUpColumnInfoCollection coll = lptPosition.Properties.Columns;
            coll.Add(new LookUpColumnInfo("Position_ID", 0, "ID"));
            coll.Add(new LookUpColumnInfo("Position_Name", 0, ""));
            lptPosition.Properties.Columns[0].Visible = false;
            lptPosition.Properties.SearchMode = SearchMode.AutoComplete;
            lptPosition.Properties.AutoSearchColumnIndex = 1;
            lptPosition.Properties.ShowHeader = false;

        }
        void LoadLookUpEdit_Vocative()
        {

            DataTable dt = new DataTable();
            Category_BLL category_BLL = new Category_BLL(Entities.EnumCategory.Vocative);
            dt = category_BLL.GetALLActivated();
            DataRow dr = dt.NewRow();
            dr["Vocative_ID"] = 0;
            dr["Vocative_Name"] = "Tất cả";
            dt.Rows.InsertAt(dr, 0);
            lptVocative.Properties.DataSource = dt;
            lptVocative.Properties.DisplayMember = "Vocative_Name";
            lptVocative.Properties.ValueMember = "Vocative_ID";
            LookUpColumnInfoCollection coll = lptVocative.Properties.Columns;
            coll.Add(new LookUpColumnInfo("Vocative_ID", 0, "ID"));
            coll.Add(new LookUpColumnInfo("Vocative_Name", 0, ""));
            lptVocative.Properties.Columns[0].Visible = false;
            lptVocative.Properties.SearchMode = SearchMode.AutoComplete;
            lptVocative.Properties.AutoSearchColumnIndex = 1;
            lptVocative.Properties.ShowHeader = false;

        }
        void LoadLookUpEdit_CustomerGroup()
        {

            DataTable dt = new DataTable();
            Category_BLL category_BLL = new Category_BLL(Entities.EnumCategory.CustomerGroup);
            dt = category_BLL.GetALLActivated();
            DataRow dr = dt.NewRow();
            dr["CustomerGroup_ID"] = 0;
            dr["CustomerGroup_Name"] = "Tất cả";
            dt.Rows.InsertAt(dr, 0);
            lptCustomerGroup.Properties.DataSource = dt;
            lptCustomerGroup.Properties.DisplayMember = "CustomerGroup_Name";
            lptCustomerGroup.Properties.ValueMember = "CustomerGroup_ID";
            LookUpColumnInfoCollection coll = lptCustomerGroup.Properties.Columns;
            coll.Add(new LookUpColumnInfo("CustomerGroup_ID", 0, "ID"));
            coll.Add(new LookUpColumnInfo("CustomerGroup_Name", 0, ""));
            lptCustomerGroup.Properties.Columns[0].Visible = false;
            lptCustomerGroup.Properties.SearchMode = SearchMode.AutoComplete;
            lptCustomerGroup.Properties.AutoSearchColumnIndex = 1;
            lptCustomerGroup.Properties.ShowHeader = false;

        }
        void LoadLookUpEdit_CustomerType()
        {

            DataTable dt = new DataTable();
            Category_BLL category_BLL = new Category_BLL(Entities.EnumCategory.CustomerType);
            dt = category_BLL.GetALLActivated();
            DataRow dr = dt.NewRow();
            dr["CustomerType_ID"] = 0;
            dr["CustomerType_Name"] = "Tất cả";
            dt.Rows.InsertAt(dr, 0);
            lptCustomerType.Properties.DataSource = dt;
            lptCustomerType.Properties.DisplayMember = "CustomerType_Name";
            lptCustomerType.Properties.ValueMember = "CustomerType_ID";
            LookUpColumnInfoCollection coll = lptCustomerType.Properties.Columns;
            coll.Add(new LookUpColumnInfo("CustomerType_ID", 0, "ID"));
            coll.Add(new LookUpColumnInfo("CustomerType_Name", 0, ""));
            lptCustomerType.Properties.Columns[0].Visible = false;
            lptCustomerType.Properties.SearchMode = SearchMode.AutoComplete;
            lptCustomerType.Properties.AutoSearchColumnIndex = 1;
            lptCustomerType.Properties.ShowHeader = false;

        }
        void LoadLookUpEdit_City()
        {

            DataTable dt = new DataTable();
            Category_BLL category_BLL = new Category_BLL(Entities.EnumCategory.City);
            dt = category_BLL.GetALLActivated();
            DataRow dr = dt.NewRow();
            dr["City_ID"] = 0;
            dr["City_Name"] = "Tất cả";
            dt.Rows.InsertAt(dr, 0);
            lptCity.Properties.DataSource = dt;
            lptCity.Properties.DisplayMember = "City_Name";
            lptCity.Properties.ValueMember = "City_ID";
            LookUpColumnInfoCollection coll = lptCity.Properties.Columns;
            coll.Add(new LookUpColumnInfo("City_ID", 0, "ID"));
            coll.Add(new LookUpColumnInfo("City_Name", 0, ""));
            lptCity.Properties.Columns[0].Visible = false;
            lptCity.Properties.SearchMode = SearchMode.AutoComplete;
            lptCity.Properties.AutoSearchColumnIndex = 1;
            lptCity.Properties.ShowHeader = false;

        }
        void LoadLookUpEdit_SourceInfo()
        {

            DataTable dt = new DataTable();
            Category_BLL category_BLL = new Category_BLL(Entities.EnumCategory.SourceInfo);
            dt = category_BLL.GetALLActivated();
            DataRow dr = dt.NewRow();
            dr["SourceInfo_ID"] = 0;
            dr["SourceInfo_Name"] = "Tất cả";
            dt.Rows.InsertAt(dr, 0);
            lptSourceInfo.Properties.DataSource = dt;
            lptSourceInfo.Properties.DisplayMember = "SourceInfo_Name";
            lptSourceInfo.Properties.ValueMember = "SourceInfo_ID";
            LookUpColumnInfoCollection coll = lptSourceInfo.Properties.Columns;
            coll.Add(new LookUpColumnInfo("SourceInfo_ID", 0, "ID"));
            coll.Add(new LookUpColumnInfo("SourceInfo_Name", 0, ""));
            lptSourceInfo.Properties.Columns[0].Visible = false;
            lptSourceInfo.Properties.SearchMode = SearchMode.AutoComplete;
            lptSourceInfo.Properties.AutoSearchColumnIndex = 1;
            lptSourceInfo.Properties.ShowHeader = false;

        }
        void LoadLookUpEdit_User()
        {

            DataTable dt = new DataTable();
            Category_BLL category_BLL = new Category_BLL(Entities.EnumCategory.Users);
            dt = category_BLL.GetALLActivated();
            DataRow dr = dt.NewRow();
            dr["Users_ID"] = 0;
            dr["Users_Name"] = "Tất cả";
            dt.Rows.InsertAt(dr, 0);
            lptUser.Properties.DataSource = dt;
            lptUser.Properties.DisplayMember = "Users_Name";
            lptUser.Properties.ValueMember = "Users_ID";
            LookUpColumnInfoCollection coll = lptUser.Properties.Columns;
            coll.Add(new LookUpColumnInfo("Users_ID", 0, "ID"));
            coll.Add(new LookUpColumnInfo("Users_Name", 0, ""));
            lptUser.Properties.Columns[0].Visible = false;
            lptUser.Properties.SearchMode = SearchMode.AutoComplete;
            lptUser.Properties.AutoSearchColumnIndex = 1;
            lptUser.Properties.ShowHeader = false;

        }
        void LoadLookUpEdit_Nationlaty()
        {

            DataTable dt = new DataTable();
            Category_BLL category_BLL = new Category_BLL(Entities.EnumCategory.Nationlaty);
            dt = category_BLL.GetALLActivated();
            DataRow dr = dt.NewRow();
            dr["Nationlaty_ID"] = 0;
            dr["Nationlaty_Name"] = "Tất cả";
            dt.Rows.InsertAt(dr, 0);
            lptNationlaty.Properties.DataSource = dt;
            lptNationlaty.Properties.DisplayMember = "Nationlaty_Name";
            lptNationlaty.Properties.ValueMember = "Nationlaty_ID";
            LookUpColumnInfoCollection coll = lptNationlaty.Properties.Columns;
            coll.Add(new LookUpColumnInfo("Nationlaty_ID", 0, "ID"));
            coll.Add(new LookUpColumnInfo("Nationlaty_Name", 0, ""));
            lptNationlaty.Properties.Columns[0].Visible = false;
            lptNationlaty.Properties.SearchMode = SearchMode.AutoComplete;
            lptNationlaty.Properties.AutoSearchColumnIndex = 1;
            lptNationlaty.Properties.ShowHeader = false;

        }
        void LoadLookUpEdit_MasketSegment()
        {

            DataTable dt = new DataTable();
            Category_BLL category_BLL = new Category_BLL(Entities.EnumCategory.MarketSegment);
            dt = category_BLL.GetALLActivated();
            DataRow dr = dt.NewRow();
            dr["MarketSegment_ID"] = 0;
            dr["MarketSegment_Name"] = "Tất cả";
            dt.Rows.InsertAt(dr, 0);
            lptMasketSegment.Properties.DataSource = dt;
            lptMasketSegment.Properties.DisplayMember = "MarketSegment_Name";
            lptMasketSegment.Properties.ValueMember = "MarketSegment_ID";
            LookUpColumnInfoCollection coll = lptMasketSegment.Properties.Columns;
            coll.Add(new LookUpColumnInfo("MarketSegment_ID", 0, "ID"));
            coll.Add(new LookUpColumnInfo("MarketSegment_Name", 0, ""));
            lptMasketSegment.Properties.Columns[0].Visible = false;
            lptMasketSegment.Properties.SearchMode = SearchMode.AutoComplete;
            lptMasketSegment.Properties.AutoSearchColumnIndex = 1;
            lptMasketSegment.Properties.ShowHeader = false;

        }

        private void btnSave_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!CheckValid())
                return;

            FormGlobals.ShowWaitForm();
            FormToData();

            //if (customer_BLL.Exists(customer))
            //{
            //    ErrorProvider_SetError(txtCode, "Mã khách hàng đã tồn tại.");

            //    FormGlobals.Message_Warning(txtCode.Text + " đã tồn tại.", "Cảnh báo");
            //    return;
            //}

            if (customerID == 0)
            {
                
                customerID = customer_BLL.Insert(customer);
                if (customerID > 0)
                {
                    FormGlobals.Message_Information("Thêm thành công", "Thông báo");
                    this.DialogResult = DialogResult.OK;
                }
                else
                    this.DialogResult = DialogResult.Cancel;
            }
            else
            {
                if (customer_BLL.Update(customer))
                {
                    FormGlobals.Message_Information("Sửa thành công", "Thông báo");
                    this.DialogResult = DialogResult.OK;
                }
                else
                    this.DialogResult = DialogResult.Cancel;
            }
            FormGlobals.CloseWaitForm();
        }

    }
}
